package com.margiel.rozkladowka.writer.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;

import com.margiel.rozkladowka.RozkladowkaMaker;
import com.margiel.rozkladowka.model.Stop;
import com.margiel.rozkladowka.model.TimeTable;
import com.margiel.rozkladowka.model.Vehicle;
import com.margiel.rozkladowka.writer.Performer;

public class PerformerImpl implements Performer {
	public final Logger log = Logger.getLogger(RozkladowkaMaker.class);
	private final static String VEHICLE_SC="V";
	public final static String STOP_SC="S";
	@Override
	public String performVehiclesString(List<Vehicle> vehicles) {
		log.debug("[performVehiclesString], entering...");
		if (CollectionUtils.isEmpty(vehicles)){
			log.info("[performVehiclesString], vehicles empty, returning");
			return null;
		}
		StringBuilder sb = new StringBuilder();
		int i=0;
		sb.append("[VEHICLES]\n");
		for (Vehicle vehicle : vehicles) {
			sb.append("["+VEHICLE_SC+(i++)+"] ["+vehicle.getNo()+"];");
		}
		sb.append("[/VEHICLES]\n");
		return sb.toString();
	}
	
	public List<String> getStopList(List<Vehicle> vehicles){
		log.debug("[getStopList], entering...");
		Set<String> stops = new HashSet<String>(5000);
		for (Vehicle vehicle : vehicles) {
			for (TimeTable timeTable : vehicle.getTimeTables()) {
				for (Stop stop : timeTable.getStops()) {
					String stopName = stop.getName().intern();
					stops.add(stopName);
				}
			}
		}
		log.debug("[getStopList], got stops=["+stops.size()+"]");
		List<String> stopsList = new ArrayList<String>(stops);
		Collections.sort(stopsList);
		return stopsList;
	}
	
	public String[] performStopsString(List<String> stopsNames){
		log.debug("[performStopsString], entering...");
		String[] result = new String[stopsNames.size()+2];
//		StringBuilder sb = new StringBuilder("[STOPS]\n");
		int i=0;
		result[i++]="[STOPS]\n";
		for (String stop : stopsNames) {
			result[i]="["+STOP_SC+(i++)+"] ["+stop+"]\n";
//			sb.append("["+STOP_SC+(i++)+"] ["+stop+"]");
		}
		result[i]="[/STOPS]\n";

//		sb.append("[/STOPS]");
		return result;
//		return sb.toString();
	}
	
//	public List<String> getDirectionEnds(List<Vehicle> vehicles){
//		
//	}

}
